/**
 * Created by Administrator on 2017/7/21.
 */
define(['jquery', "vue", 'ks-spa'], function ($, Vue, spa) {
//调用微信JS api 支付
    function jsApiCall() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            jsApiParameters,
            function (res) {
                console.log(res);
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    setTimeout("history.back()", 500);
                    //window.location.href = user_url;
                }else {
                    console.log(res.err_msg);
                }
            }
        );
    }

    var money = 0;
    var jsApiParameters;
    var vue = new Vue({
        el: '#pages',
        data: {},
        methods: {
            pay: function () {
                money = document.getElementById("money").value;
                if (money === '') {
                    mui.alert('请输入金额');
                    return;
                }
                if(money<0){
                    mui.alert('充值金额错误！');
                    return;
                }
                spa.showLoading();
                var data = {
                    money: money
                };
                $.post(recharge_url, data, function (res) {
                    spa.hideLoading();
                    if (res.code === 1) {
                        jsApiParameters = res.data;
                        console.log(jsApiParameters);
                        if (typeof WeixinJSBridge == "undefined") {
                            if (document.addEventListener) {
                                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                            } else if (document.attachEvent) {
                                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                            }
                        } else {
                            jsApiCall();
                        }
                    } else {
                        mui.alert(res.msg, '系统提示');
                    }
                });
            }
        }
    });
});
